#ifndef GTAXIS_H
#define GTAXIS_H

#include "BasicElement/scaxis.h"
#include "gtaxisconfig.h"
#include "gtresulthandler.h"
#include "gts.h"

#define VelCoeff (1000)
#define AccCoeff (1000000)

class GTAxis : public SCAxis
{
    Q_OBJECT

public:
    enum MoveMode
    {
        Unknown,
        Pos,
        Jog
    };

    GTAxis(QString name, QObject *parent = nullptr);
    GTAxisConfig *getGtAxisConfig() const
    {
        return qobject_cast<GTAxisConfig *>(config());
    }
    void setMoveMode(MoveMode mode, bool checkCurrentMode = true);

    // SCAxis interface
public:
    virtual double getCurrentOutputPos() override;
    virtual void setFeedbackPos(double pos) override;
    virtual void setCurrentPos(double pos) override;
    virtual bool hasAlarm() override;
    virtual bool isReady() const override
    {
        return true;
    }
    virtual bool isInPos() const override;
    virtual bool isRunning() const override;
    virtual double positiveLimit() const override
    {
        return gtAxisConfig->positiveLimit();
    }
    virtual double negativeLimit() const override
    {
        return gtAxisConfig->negativeLimit();
    }
    virtual double maxVelocity() const override
    {
        return gtAxisConfig->maxVel();
    }
    virtual double getCurrentVel() override;

    virtual void clearStatus() override;

protected:
    virtual void initImpl() override;
    virtual void homeImpl() override;
    virtual bool isHomeDone() override;
    virtual QString homeErrorMsg() override;
    virtual void operationAfterHome() override;
    virtual void stopImpl() override;
    virtual void stopHome() override;
    virtual void enableImpl() override;
    virtual void disableImpl() override;
    virtual void clearErrorImpl() override;
    virtual void moveToImpl(double targetPos) override;
    virtual void velocityMoveImpl(Direction dir) override;
    virtual double getFeedbackPosImpl() override;
    virtual void changeTargetPosOnlineImpl(double targetPos) override;
    virtual bool bindToMasterAxisImpl(bool bind) override
    {
        // Todo
        qCritical() << "UnImplemented function...";
        return false;
    }
    virtual void scaleMaxAccImpl(double ratio) override;

protected slots:
    virtual void scaleMaxVelImpl(double ratio) override;

private:
    MoveMode currentMode = Unknown;
    GTAxisConfig *gtAxisConfig = nullptr;
    double lastAccRatio = 1;
    double lastVelRatio = 1;
    short coreNo;
    short index;
    THomeStatus homeStatus;
};

#endif    // GTAXIS_H
